package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.TaskfasBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YanShouRenWuActivity extends BaseActivity implements View.OnClickListener {

    private RecyclerView recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yan_shou_ren_wu);
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("任务榜");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        findViewById(R.id.ll_search).setOnClickListener(this);
        findViewById(R.id.ll_search).setVisibility(View.VISIBLE);
        onUpLoad("");
    }

    private void onUpLoad(String str) {
        Map<String, Object> map = new HashMap<>();
        map.put("keywords", str);
        showDialog();

        XUtil.Post(Url.DECORATE_TASKLIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        TaskfasBean test = gson.fromJson(result, TaskfasBean.class);
                        List<TaskfasBean.InfoEntity> info1 = test.getInfo();
                        set(info1);
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });


    }

    private void set(final List<TaskfasBean.InfoEntity> info1) {
        recycler.setAdapter(new BaseRVAdapter(this, info1) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_yanshou_renwu_new;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                x.image().bind(holder.getImageView(R.id.tou), info1.get(position).getHeadimg());
                holder.setTextView(R.id.name, info1.get(position).getRealname());
                holder.setTextView(R.id.company, info1.get(position).getCompany());
                holder.setTextView(R.id.yanshoumingzi, info1.get(position).getLevel());
                holder.setTextView(R.id.sj, "手机号：" + info1.get(position).getTelephone());

                RecyclerView recycler = holder.getView(R.id.recycler);
                recycler.setLayoutManager(new LinearLayoutManager(YanShouRenWuActivity.this, LinearLayoutManager.VERTICAL, false));
                final List<TaskfasBean.InfoEntity.OrderEntity> order = info1.get(position).getOrder();
                recycler.setAdapter(new BaseRVAdapter(YanShouRenWuActivity.this, order) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.item_yanshou_renwu_new2;
                    }

                    @Override
                    public void onBind(BaseViewHolder holder, int position) {
                        x.image().bind(holder.getImageView(R.id.imge), order.get(position).getImg());
                        holder.setTextView(R.id.hetong, "合同号：" + order.get(position).getBudget_no());
                        holder.setTextView(R.id.com, order.get(position).getBudget_name());
                        holder.setTextView(R.id.type, order.get(position).getLabel());
                        holder.setTextView(R.id.addre, order.get(position).getAddress());
                        holder.setTextView(R.id.has, "总套数：" + order.get(position).getAll_nums() + "套/已报检套数：" + order.get(position).getHas_nums() + "套");
                        holder.setTextView(R.id.time, order.get(position).getAdd_time());

                    }


                });
            }


        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.ll_search:
                startActivity(new Intent(this, SearchDKASActivity.class));
                break;
        }
    }
}
